//
//  Yahoo_Quote_Mobile.m
//  MacStockApp
//
//  Created by 英傑 翁 on 12/3/5.
//  Copyright (c) 2012年 翁英傑. All rights reserved.
//

#import "Yahoo_Quote_Mobile.h"
#import "CommonFunction.h"
#import "GBTableColumnHeader.h"
#import "GBTableColumnHeader_basicSqlite.h"
#import "GBTableColumnHeader_quote.h"
#import "GBTableColumnHeader_2Row3Row.h"
#import "GBTableColumnHeader_node.h" 
#import "GBTableColumnHeader_position.h"  
#import "GBTableColumnHeader_position_bottom_sumaverage.h"  
#import "GBTableColumnHeader_Indicator.h"  
#import "GBTableColumnHeader_signal.h"  
#import "GBTableColumnHeader_ObjectOtherKey.h"  
#import "XXcrackServerRequest.h"

@implementation Yahoo_Quote_Mobile
@synthesize delgeate;
- (id)init_WithURL:(NSURL*)url; {
	
    if(self=[super init])
	{
         
        targetURL= url  ;
        [targetURL retain];
    }
	
	return self;
}

//標記：建立執行緒 3.執行緒內的啟動- 在 addOperation 後面執行

- (void)main 
{ 
    NSLog(@"main_Start");
    
    
    //3.Header Field 欺騙bloomberg在User-Agent
    
    
    NSURL* url = targetURL;
    
    //Yahoo Mobile Server 破解指令
    NSMutableURLRequest* request  ;
    request= [XXcrackServerRequest Yahoo_Mobile_Crack_Request:url] ;
       
    
    NSURLResponse* response = nil;
    NSError* error = nil;
    NSData* data = [NSURLConnection sendSynchronousRequest:request
                                         returningResponse:&response
                                                     error:&error];    
    
    NSString* aStr;
	aStr = [[NSString alloc] initWithData:data encoding:NSASCIIStringEncoding];
 
    //NSLog(@"download=%@",aStr);
    
    //分解   
    NSDictionary *dic_finance =  [[aStr JSONValue] objectForKey:@"finance"] ;
    [aStr release];
    NSDictionary *dic_Quote=[dic_finance  objectForKey:@"quotes"];
    NSArray *array_Quote=[ dic_Quote  objectForKey:@"Quote"];    
    
   //NSLog(@"array_Quote=%@",array_Quote);
   
    NSMutableArray *outarray=[[NSMutableArray alloc] init];
    
    //1.建構報價資料陣列
    for (NSDictionary *dds in array_Quote ) {
        NSMutableDictionary *tem_dic=[[NSMutableDictionary alloc] init];        
        
        
        //1.下面資料是yahoo,bloomberg 共同有的報價資料
        NSString *code_s=[dds objectForKey:@"symbol"];
               
        NSDate *datetime_dt=[NSDate dateWithTimeIntervalSince1970:[[dds objectForKey:@"ts"] doubleValue] ];   
        NSNumber *open_num=[NSNumber numberWithDouble: [[dds objectForKey:@"open"] doubleValue] ];
        NSNumber *high_num=[NSNumber numberWithDouble: [[dds objectForKey:@"day_high"] doubleValue] ];        
        NSNumber *low_num=[NSNumber numberWithDouble: [[dds objectForKey:@"day_low"] doubleValue] ];
        NSNumber *close_num=[NSNumber numberWithDouble: [[dds objectForKey:@"price"] doubleValue] ];        
        NSNumber *updown_num=[NSNumber numberWithDouble: [[dds objectForKey:@"change"] doubleValue] ];
        NSNumber *updown_p_num=[NSNumber numberWithDouble: [[dds objectForKey:@"chg_percent"] doubleValue] ];
        NSNumber *volume_num=[NSNumber numberWithDouble: [[dds objectForKey:@"volume"] doubleValue] ];        
        NSNumber *PE_num=[NSNumber numberWithDouble: [[dds objectForKey:@"pe_ratio"] doubleValue] ]; 
        NSNumber *MarketCap_num=[NSNumber numberWithDouble: [[dds objectForKey:@"market_cap"] doubleValue] ];       
        NSNumber *low_52week_num=[NSNumber numberWithDouble: [[dds objectForKey:@"year_low"] doubleValue] ];
        NSNumber *high_52week_num=[NSNumber numberWithDouble: [[dds objectForKey:@"year_high"] doubleValue] ];        
                
        
        //標記：報價-資料是透過網路上取出報價資料來載入_all 4.yahoo mobile 資料輸入               
        [tem_dic setObject:code_s forKey:DB_object_sqlite_basic_code];
        [tem_dic setObject:datetime_dt forKey:DB_object_quote_all_datetime];         
        [tem_dic setObject:open_num forKey:DB_object_quote_all_open];
        [tem_dic setObject:close_num forKey:DB_object_quote_all_close];
        [tem_dic setObject:high_num forKey:DB_object_quote_all_high];
        [tem_dic setObject:low_num forKey:DB_object_quote_all_low];
        [tem_dic setObject:updown_num forKey:DB_object_quote_all_updown];
        [tem_dic setObject:updown_p_num forKey:DB_object_quote_all_updown_p];
        [tem_dic setObject:volume_num forKey:DB_object_quote_all_vol];
        [tem_dic setObject:PE_num forKey:DB_object_quote_all_PE];
        [tem_dic setObject:MarketCap_num forKey:DB_object_quote_all_MarketCap];         
        [tem_dic setObject:high_52week_num forKey:DB_object_quote_all_high_52week];   
        [tem_dic setObject:low_52week_num forKey:DB_object_quote_all_low_52week];
        [tem_dic setObject:[NSNumber numberWithDouble: 999 ] forKey:DB_object_quote_all_sample1];       
        
           
        
        //2.下面資料是YAHOO 獨有
        //標記：報價-資料是透過網路上取出報價資料來載入_yahoo 4.yahoo mobile 資料輸入 888        
        {
            
            NSString *currency_s=@"";
            NSString *exchange_s=@"";
            
            
            
            NSNumber *dividend_rate_num=[NSNumber numberWithDouble: 0 ]; 
            NSNumber *dividend_yield_num=[NSNumber numberWithDouble:0 ];       
            NSNumber *eps_curr_year_num=[NSNumber numberWithDouble: 0];
            NSNumber *realtime_change_num=[NSNumber numberWithDouble: 0 ]; 
            NSNumber *realtime_chg_percent_num=[NSNumber numberWithDouble: 0];       
            NSNumber *realtime_price_num=[NSNumber numberWithDouble: 0];
            NSDate   *realtime_ts=[NSDate dateWithTimeIntervalSince1970:0 ]; 
            
            NSNumber *after_mkt_change_num=[NSNumber numberWithDouble: 0 ]; 
            NSNumber *after_mkt_chg_percent_num=[NSNumber numberWithDouble:0 ];       
            NSNumber *after_mkt_price_num=[NSNumber numberWithDouble:0 ];
            
            
            if ([dds objectForKey:@"after_mkt_change"]) {
                after_mkt_change_num=[NSNumber numberWithDouble: [[dds objectForKey:@"after_mkt_change"] doubleValue] ];
            }
            if ([dds objectForKey:@"after_mkt_chg_percent"]) {
                after_mkt_chg_percent_num=[NSNumber numberWithDouble: [[dds objectForKey:@"after_mkt_chg_percent"] doubleValue] ];         
            }
            if ([dds objectForKey:@"after_mkt_price"]) {
                after_mkt_price_num=[NSNumber numberWithDouble: [[dds objectForKey:@"after_mkt_price"] doubleValue] ];
            }
            if ([dds objectForKey:@"realtime_change"]) {
                realtime_change_num=[NSNumber numberWithDouble: [[dds objectForKey:@"realtime_change"] doubleValue] ]; 
            }
            if ([dds objectForKey:@"realtime_chg_percent"] ) {
                realtime_chg_percent_num=[NSNumber numberWithDouble: [[dds objectForKey:@"realtime_chg_percent"] doubleValue] ];
            }
            if ([dds objectForKey:@"realtime_price"] ) {
                realtime_price_num=[NSNumber numberWithDouble: [[dds objectForKey:@"realtime_price"] doubleValue] ];
            }
            if ([dds objectForKey:@"realtime_ts"]) {
                realtime_ts=[NSDate dateWithTimeIntervalSince1970:[[dds objectForKey:@"realtime_ts"] doubleValue] ];   
            }
            if ([dds objectForKey:@"currency"]) {
                currency_s=[dds objectForKey:@"currency"];
            }
            if ([dds objectForKey:@"exchange"]) {
                exchange_s=[dds objectForKey:@"exchange"];
            }
            if ([dds objectForKey:@"dividend_rate"] ) {
                dividend_rate_num=[NSNumber numberWithDouble: [[dds objectForKey:@"dividend_rate"] doubleValue] ]; 
            }
            if ([dds objectForKey:@"dividend_yield"] ) {
                dividend_yield_num=[NSNumber numberWithDouble: [[dds objectForKey:@"dividend_yield"] doubleValue] ];   
            }        
            if ([dds objectForKey:@"eps_curr_year"] ) {
                eps_curr_year_num=[NSNumber numberWithDouble: [[dds objectForKey:@"eps_curr_year"] doubleValue] ];  
            }
            
            
            [tem_dic setObject:after_mkt_change_num forKey:DB_object_quote_Yahoo_after_mkt_change];   
            [tem_dic setObject:after_mkt_chg_percent_num forKey:DB_object_quote_Yahoo_after_mkt_chg_percent];
            [tem_dic setObject:after_mkt_price_num forKey:DB_object_quote_Yahoo_after_mkt_price];             
            [tem_dic setObject:currency_s forKey:DB_object_quote_Yahoo_currency];   
            [tem_dic setObject:exchange_s forKey:DB_object_quote_Yahoo_exchange];
            [tem_dic setObject:dividend_rate_num forKey:DB_object_quote_Yahoo_dividend_rate];               
            [tem_dic setObject:dividend_yield_num forKey:DB_object_quote_Yahoo_dividend_yield];         
            [tem_dic setObject:eps_curr_year_num forKey:DB_object_quote_Yahoo_eps_curr_year]; 
            [tem_dic setObject:realtime_change_num forKey:DB_object_quote_Yahoo_realtime_change];   
            [tem_dic setObject:realtime_chg_percent_num forKey:DB_object_quote_Yahoo_realtime_chg_percent];
            [tem_dic setObject:realtime_price_num forKey:DB_object_quote_Yahoo_realtime_price];               
            [tem_dic setObject:realtime_ts forKey:DB_object_quote_Yahoo_realtime_ts];                
            [tem_dic setObject:[NSNumber numberWithDouble:888 ] forKey:DB_object_quote_Yahoo_sample1];          
        
        }
        
              
        
        //3.下面資料是bloomberg 獨有
        //標記：報價-資料是透過網路上取出報價資料來載入_bloomberg 4.yahoo mobile 資料輸入 輸入假的值  111
        {
            NSString *introduce=@"";        
            NSNumber *return_52week_num=[NSNumber numberWithDouble: 0 ];        
            [tem_dic setObject:introduce forKey:DB_object_quote_Bloomberg_introduce];        
            [tem_dic setObject:return_52week_num forKey:DB_object_quote_Bloomberg_return_52week];          
            [tem_dic setObject:[NSNumber numberWithDouble:111 ] forKey:DB_object_quote_Bloomberg_sample1];         
        
        }
       
        
        //4.Google特有的資料
        //標記：報價-資料是透過網路上取出報價資料來載入_google 4.yahoo mobile 資料輸入 輸入假的值111        
        {
            NSDate *Google_after_mkt_time =[NSDate dateWithTimeIntervalSince1970:9999];       
            NSNumber *Google_after_mkt_price_num=[NSNumber numberWithDouble: 0 ];         
            NSNumber *Google_after_mkt_change_num=[NSNumber numberWithDouble: 0 ]; 
            NSNumber *Google_after_mkt_chg_percent_num=[NSNumber numberWithDouble: 0 ];         
            NSNumber *Google_shares_num=[NSNumber numberWithDouble: 0 ];         
            NSNumber *Google_beta_num=[NSNumber numberWithDouble: 0 ]; 
            NSNumber *Google_institution_own_num=[NSNumber numberWithDouble: 0 ];           
            NSNumber *Google_delay_num=[NSNumber numberWithDouble: 0 ];         
            NSNumber *Google_30average_vol_num=[NSNumber numberWithDouble: 0 ]; 
            NSNumber *Google_timezone=[NSNumber numberWithDouble: 0 ];        
            
            [tem_dic setObject:Google_after_mkt_time forKey:DB_object_quote_Google_after_mkt_time];         
            [tem_dic setObject:Google_after_mkt_price_num forKey:DB_object_quote_Google_after_mkt_price];  
            [tem_dic setObject:Google_after_mkt_change_num forKey:DB_object_quote_Google_after_mkt_change];         
            [tem_dic setObject:Google_after_mkt_chg_percent_num forKey:DB_object_quote_Google_after_mkt_chg_percent];  
            [tem_dic setObject:Google_shares_num forKey:DB_object_quote_Google_shares];         
            [tem_dic setObject:Google_beta_num forKey:DB_object_quote_Google_beta];  
            [tem_dic setObject:Google_institution_own_num forKey:DB_object_quote_Google_institution_own];         
            [tem_dic setObject:Google_delay_num forKey:DB_object_quote_Google_delay];  
            [tem_dic setObject:Google_30average_vol_num forKey:DB_object_quote_Google_30average_vol];      
            [tem_dic setObject:Google_timezone forKey:DB_object_quote_Google_timezone];      
            
            
                    
            [tem_dic setObject:[NSNumber numberWithInt:111] forKey:DB_object_quote_Google_sample1];        
            
        }
              //最後添加到陣列
        [outarray addObject:tem_dic];          
        [tem_dic release];
    
    
    }
   
    //2.將報價資料陣列更新到物件上
    [CommonFunction after_quotefinished_outarray:outarray delgeate:delgeate];     
    
    //標記：建立執行緒 4.執行緒內資料計算結束後 要通知delgete    
    [(Data_Model *)delgeate performSelectorOnMainThread:@selector(Quote_Finished:) withObject:outarray waitUntilDone:YES];    
    
    
         
    [outarray release];     
    
}
 


@end
